﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
   /// <summary>
   /// Interaction logic for NewSensor.xaml
   /// </summary>
   public partial class NewSensor : Window
   {
      /// <summary>
      /// The new sensor to be added to an account's sensor list.
      /// </summary>
      public Classes.Sensor toBeAdded;

      /// <summary>
      /// Sets up the DataContext of the combobox to the static list of
      /// equipment in EquipmentList.
      /// </summary>
      public NewSensor()
      {
         InitializeComponent();

         combo_SensorType.DataContext = Classes.EquipmentList.List;
      }

      /// <summary>
      /// Get the value for a Common Location selection.
      /// </summary>
      /// <returns>The value for the selected RadRadioButton</returns>
      private String GetLocation()
      {
         String selected = "";

         if (radio_FD.IsChecked == true)
            selected = "Front Door";
         else if (radio_BD.IsChecked == true)
            selected = "Back Door";
         else if (radio_BM.IsChecked == true)
            selected = "Basement";
         else if (radio_HL.IsChecked == true)
            selected = "Hall";
         else if (radio_KT.IsChecked == true)
            selected = "Kitchen";
         else if (radio_LR.IsChecked == true)
            selected = "Living Room";
         else if (radio_MB.IsChecked == true)
            selected = "Master Bedroom";
         else if (radio_SD.IsChecked == true)
            selected = "Side Door";
         else if (radio_UP.IsChecked == true)
            selected = "Upstairs";
         else selected = text_Location.Text;

         return selected;
      }

      /// <summary>
      /// Uses a regex match to check validity of upgraded price.
      /// </summary>
      /// <param name="sender">TextBox, Name: text_Upgrade</param>
      /// <param name="e"></param>
      private void text_Upgrade_LostFocus(object sender, RoutedEventArgs e)
      {
         if ( Regex.IsMatch( (sender as TextBox).Text, "-?[0-9]*(.[0-9]*)?" ) )
         {
            (sender as TextBox).Text = String.Format("{0:C}", (sender as TextBox).Text);
         }
         else
         {
            MessageBox.Show("Invalid price. Please use ONLY numbers and decimals. Examples: \"20.00\", \"25\"");
            (sender as TextBox).Text = "";
         }
      }

      /// <summary>
      /// Called when the "Add sensor" button is clicked. Creates a new Sensor 
      /// object from the input and sets the DialogResult to true.
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void CreateNewSensor(object sender, RoutedEventArgs e)
      {
         DialogResult = true;

         toBeAdded = new Classes.Sensor( (combo_SensorType.SelectedItem as Classes.Equipment).PartNumber, 
            GetLocation(), check_Existing.IsChecked == true ? true : false , (int)number_Zone.Value,
            (check_Upgrade.IsChecked == true ? Double.Parse(text_Upgrade.Text) : -1) );

         Hide();
      }

      /// <summary>
      /// Enables the Upgrade TextBox when the Upgrade CheckBox is checked.
      /// </summary>
      /// <param name="sender">CheckBox, Name: check_Upgrade</param>
      /// <param name="e"></param>
      private void check_Upgrade_Checked(object sender, RoutedEventArgs e)
      {
         text_Upgrade.IsEnabled = true;
      }

      /// <summary>
      /// Disables the Upgrade TextBox when the Upgrade CheckBox is unchecked.
      /// </summary>
      /// <param name="sender">CheckBox, Name: check_Upgrade</param>
      /// <param name="e"></param>
      private void check_Upgrade_Unchecked(object sender, RoutedEventArgs e)
      {
         text_Upgrade.IsEnabled = false;
      }

      /// <summary>
      /// Enables the Location TextBox when the "Other" radio button is selected.
      /// </summary>
      /// <param name="sender">RadRadioButton, Name: radio_OT</param>
      /// <param name="e"></param>
      private void radio_OT_Checked(object sender, RoutedEventArgs e)
      {
         text_Location.IsEnabled = true;
      }

      /// <summary>
      /// Disables the Location TextBox when the "Other" radio button is not selected.
      /// </summary>
      /// <param name="sender">RadRadioButton, Name: radio_OT</param>
      /// <param name="e"></param>
      private void radio_OT_Unchecked(object sender, RoutedEventArgs e)
      {
         text_Location.IsEnabled = false;
      }
   }
}
